package com.xjm.resource;

import org.springframework.core.io.UrlResource;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URI;

/**
 * @author jaymin
 * 2020/12/6 20:56
 */
public class URLResourceDemo {
	public static void main(String[] args) throws IOException {
		UrlResource urlResource = new UrlResource("https://upload-images.jianshu.io/upload_images/19836894-f5744dda73c1dab8.png");
		String url = urlResource.getURL().toString();
		System.out.println(url);
		String filename = urlResource.getFilename();
		System.out.println(filename);
		String description = urlResource.getDescription();
		System.out.println(description);
		URI uri = urlResource.getURI();
		System.out.println(uri.toString());
		File file = new File("C:\\Users\\95152\\Desktop\\Git\\myPicture.png");
		try (InputStream inputStream = urlResource.getInputStream();
			 OutputStream outStream = new FileOutputStream(file)) {
			byte[] buffer = new byte[8 * 1024];
			int bytesRead;
			while ((bytesRead = inputStream.read(buffer)) != -1) {
				outStream.write(buffer, 0, bytesRead);
			}
		}
	}
}
